



import socket
import random
from types import *




class hell(Exception):
    def __init__(self, value="Hell!"):
        self.value = value
    def __str__(self):
        return repr(self.value)


def sure_recv(sock, n):
    if isinstance(n, StringType):
        s = sure_recv(sock, len(n))
        if s != n:
            raise hell
        return s
    s = []
    i = 0
    while i < n:
        r = sock.recv(n)
        l = len(r)
        if l == 0:
            raise hell
        s.append(r)
        i += l
    return ''.join(s)

def sure_send(sock, s):
    i = 0
    l = len(s)
    while i < l:
        r = sock.send(s[i:])
        if r <= 0:
            raise hell
        i += r
    return

def delim_recv(sock, delims):
    s = []
    r = ''
    while r not in delims:
        s.append(r)
        r = sock.recv(1)
        l = len(r)
        if l == 0:
            raise hell
    return ''.join(s)


def getnums(s):
    digits = set(map(str,range(10)))
    nums = []
    curnum = None
    s += ' '
    for c in s:
        if c in digits:
            if curnum is None:
                curnum = 0
            curnum = (curnum*10)+(ord(c)-ord(0))
        elif curnum is not None:
            nums.append(curnum)
            curnum = None
    return nums

